package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.constant.ResponseEnum;

import com.lebang.medicalInquiry.model.po.Config;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.ConfigService;

import com.lebang.medicalInquiry.utils.TextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "【WEB端】参数配置管理")
@Controller
@RequestMapping("/config")
public class ConfigController {
    @Autowired
    private ConfigService configService;


    @ApiOperation("获取配置详情信息")
    @GetMapping(value = "/value")
    @RequiresPermissions(value = {"config:value:get"})
    @ResponseBody
    public JsonData getConfig(HttpServletRequest request, HttpServletResponse response,
                              @ApiParam(name = "code", value = "类型：1：支付宝，2：微信，3：系统参数", required = true, example = "1")
                              @RequestParam(name = "code") Integer code
    ) {

        if (TextUtil.isEmpty(code)) {
            return Callback.fail(ResponseEnum.LOST_PARAMS.getCode(), ResponseEnum.LOST_PARAMS.getMemo());
        }

        String content = configService.getByCode(code);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), content);

    }


    @ApiOperation("修改配置信息")
    @PutMapping(value = "/value")
    @RequiresPermissions(value = {"config:value:put"})
    @ResponseBody
    public JsonData updateConfig(HttpServletRequest request, HttpServletResponse response,
                                 @ApiParam(name = "content", value = "配置参数和值，json格式的字符串", required = true)
                                 @RequestBody Config data
    ) {
        try {
            if (data == null || TextUtil.isEmpty(data.getCode()) || TextUtil.isEmpty(data.getValue())) {
                return Callback.fail(ResponseEnum.LOST_PARAMS.getCode(), ResponseEnum.LOST_PARAMS.getMemo());
            }

            boolean isSuccess = configService.updateConfig(data);
            if (isSuccess) {
                return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

    @ApiOperation("获取配置详情信息")
    @GetMapping("/")
    @RequiresPermissions(value = {"config:get"})
    @ResponseBody
    public JsonData getAll(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(name = "page", defaultValue = "1") Integer page,
                           @RequestParam(name = "limit", defaultValue = "10") Integer limit
    ) {
        try {
            Map<String, Object> s = configService.getAll((page - 1) * limit, limit);
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

    @ApiOperation("添加配置详情信息")
    @PostMapping("/")
    @RequiresPermissions(value = {"config:post"})
    @ResponseBody
    public JsonData add(HttpServletRequest request, HttpServletResponse response, @RequestBody Config config
    ) {
        try {
            if (config == null || TextUtil.isEmpty(config.getValue()) || TextUtil.isEmpty(config.getCode())) {
                return Callback.fail(ResponseEnum.LOST_PARAMS.getCode(), ResponseEnum.LOST_PARAMS.getMemo());
            }

            configService.add(config);
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

    @ApiOperation("修改配置详情信息")
    @PutMapping("/")
    @RequiresPermissions(value = {"config:put"})
    @ResponseBody
    public JsonData update(HttpServletRequest request, HttpServletResponse response, @RequestBody Config config
    ) {
        try {
            if (config == null || TextUtil.isEmpty(config.getId())) {
                return Callback.fail(ResponseEnum.LOST_PARAMS.getCode(), ResponseEnum.LOST_PARAMS.getMemo());
            }

            configService.update(config);
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }

    @ApiOperation("删除配置详情信息")
    @DeleteMapping("/{id}")
    @RequiresPermissions(value = {"config:delete"})
    @ResponseBody
    public JsonData remove(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id
    ) {
        try {

            configService.deleteById(id);
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.fail(ResponseEnum.UNEXCEPTION.getCode(), ResponseEnum.UNEXCEPTION.getMemo());
    }
}